package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.UniversityAdmissionInformationDto;
import xyz.zhouzhaodong.crm.entity.UniversityAdmissionInformation;
import xyz.zhouzhaodong.crm.entity.UniversityAdmissionInformation;
import xyz.zhouzhaodong.crm.mapper.UniversityAdmissionInformationMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityAdmissionInformationMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityMapper;
import xyz.zhouzhaodong.crm.service.IUniversityAdmissionInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 高校招生信息 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-23
 */
@Service
public class UniversityAdmissionInformationServiceImpl extends ServiceImpl<UniversityAdmissionInformationMapper, UniversityAdmissionInformation> implements IUniversityAdmissionInformationService {

    @Resource
    UniversityAdmissionInformationMapper universityAdmissionInformationMapper;

    @Resource
    UniversityMapper universityMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addUniversityAdmissionInformation(UniversityAdmissionInformationDto universityAdmissionInformationDto) {
        try {
            UniversityAdmissionInformation universityAdmissionInformation = dtoChange(universityAdmissionInformationDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            universityAdmissionInformation.setId(id);
            universityAdmissionInformation.setCreateUser(universityAdmissionInformationDto.getStaffId());
            universityAdmissionInformation.setCreateTime(new Date());
            universityAdmissionInformationMapper.insert(universityAdmissionInformation);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteUniversityAdmissionInformation(UniversityAdmissionInformationDto universityAdmissionInformationDto) {
        try {
            UniversityAdmissionInformation universityAdmissionInformation = new UniversityAdmissionInformation();
            universityAdmissionInformation.setId(universityAdmissionInformationDto.getId());
            universityAdmissionInformation.setUpdateUser(universityAdmissionInformationDto.getStaffId());
            universityAdmissionInformation.setUpdateTime(new Date());
            universityAdmissionInformation.setFlag(1);
            universityAdmissionInformationMapper.updateById(universityAdmissionInformation);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateUniversityAdmissionInformation(UniversityAdmissionInformationDto universityAdmissionInformationDto) {
        try {
            UniversityAdmissionInformation universityAdmissionInformation = dtoChange(universityAdmissionInformationDto);
            universityAdmissionInformation.setUpdateUser(universityAdmissionInformationDto.getStaffId());
            universityAdmissionInformation.setUpdateTime(new Date());
            universityAdmissionInformationMapper.updateById(universityAdmissionInformation);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectUniversityAdmissionInformation(UniversityAdmissionInformationDto universityAdmissionInformationDto) {
        Integer flag = universityAdmissionInformationDto.getFlag() == null ? 0 : universityAdmissionInformationDto.getFlag();
        if (universityAdmissionInformationDto.getPageNo() != 0 && universityAdmissionInformationDto.getPageSize() != 0){
            Page<UniversityAdmissionInformationDto> page = new Page<>(universityAdmissionInformationDto.getPageNo(), universityAdmissionInformationDto.getPageSize());
            universityAdmissionInformationDto.setFlag(flag);
            IPage<UniversityAdmissionInformationDto> list = universityAdmissionInformationMapper.selectListPage(page, universityAdmissionInformationDto);
            for(UniversityAdmissionInformationDto u : list.getRecords()){
                u.setUniversity(universityMapper.selectById(u.getUniversityId()));
            }
            return RestMessage.success(list);
        } else {
            QueryWrapper<UniversityAdmissionInformation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("university_id", universityAdmissionInformationDto.getUniversityId());
            queryWrapper.eq("flag", flag);
            List<UniversityAdmissionInformation> list = universityAdmissionInformationMapper.selectList(queryWrapper);
            return RestMessage.success(list);
        }
    }

    private UniversityAdmissionInformation dtoChange(UniversityAdmissionInformationDto universityAdmissionInformationDto) {
        UniversityAdmissionInformation universityAdmissionInformation = new UniversityAdmissionInformation();
        universityAdmissionInformation.setId(universityAdmissionInformationDto.getId());
        universityAdmissionInformation.setUniversityId(universityAdmissionInformationDto.getUniversityId());
        universityAdmissionInformation.setTitle(universityAdmissionInformationDto.getTitle());
        universityAdmissionInformation.setContent(universityAdmissionInformationDto.getContent());
        universityAdmissionInformation.setRemark(universityAdmissionInformationDto.getRemark());
        universityAdmissionInformation.setFlag(universityAdmissionInformationDto.getFlag());
        return universityAdmissionInformation;
    }

}
